#include <iostream>
#include <vector>
using namespace std;

class Solution
{
public:
    int findClosestNumber(vector<int>& nums)
    {
        int res = nums[0];
        int dis = abs(nums[0]);
        for (auto n : nums)
        {
            if (abs(n) < dis)
            {
                dis = abs(n);
                res = n;
            }
            else if (abs(n) == dis)
            {
                res = max(res, n);
            }
        }
        return res;
    }
};

int main()
{
    vector<int> nums = { -4, -2, 4, 8 };
    Solution solution;
    int result = solution.findClosestNumber(nums);
    cout << result << endl;
    return 0;
}